package levelrewind.android.app.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import levelrewind.android.app.DisplayBattleActivity;
import levelrewind.android.app.R;
import levelrewind.android.app.model.PictureHandler;
import levelrewind.android.app.model.Util;
import levelrewind.android.app.model.Util.Emotion;
import levelrewind.android.app.vo.Battle;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BattleListAdapter extends BaseAdapter {

	private ArrayList<Battle>  battles = new ArrayList<Battle>();
    private LayoutInflater mInflater;
    private Context c;
    
    public BattleListAdapter(Context context) {
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        c = context;
    }
    
    public void addItem(final Battle item) {
    	battles.add(item);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return battles.size();
    }

    @Override
    public Battle getItem(int position) {
        return battles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SimpleDateFormat")
	@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        System.out.println("getView " + position + " " + convertView);
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.battle_list_layout, null);
            holder = new ViewHolder();
            holder.layout = (LinearLayout)convertView.findViewById(R.id.user_data);
            holder.per1Pic = (ImageView)convertView.findViewById(R.id.person1_pic);
            holder.per2Pic = (ImageView)convertView.findViewById(R.id.person2_pic);
            holder.txtName = (TextView)convertView.findViewById(R.id.battle_name);
            holder.txtStart = (TextView)convertView.findViewById(R.id.battle_start);
            holder.playBtn = (TextView)convertView.findViewById(R.id.battle_play);
            holder.txtEnd = (TextView)convertView.findViewById(R.id.battle_end);
            
            holder.txtName.setTypeface(Util.customFont(c));
            holder.txtStart.setTypeface(Util.customFont(c));
            holder.playBtn.setTypeface(Util.customFont(c));
            holder.txtEnd.setTypeface(Util.customFont(c));
            
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        
        // Set playable control for the battles
        if (battles.get(position).getStartDate().after(new Date())) {
        	holder.playBtn.setVisibility(View.INVISIBLE);	        
        }
        else {
        	holder.playBtn.setVisibility(View.VISIBLE);
        	
        	holder.layout.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Bundle b = Util.setBundleObject(battles.get(position));
					Intent intent = new Intent(c, DisplayBattleActivity.class);
					intent.putExtras(b);
					c.startActivity(intent);
				}
			});
        }
        
        String DATE_FORMAT = "dd/MM/yyyy";       
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        
        PictureHandler.viewImageFromDevice(c, battles.get(position).getPersons()[0].getPerson(), Emotion.NORMAL, holder.per1Pic);
		PictureHandler.viewImageFromDevice(c, battles.get(position).getPersons()[1].getPerson(), Emotion.NORMAL, holder.per2Pic);
		
        holder.txtName.setText(battles.get(position).getName());
        holder.txtStart.setText(sdf.format(battles.get(position).getStartDate()));
        holder.playBtn.setTag(battles.get(position));
        holder.txtEnd.setText(sdf.format(battles.get(position).getEndDate()));

        return convertView;
    }

    public static class ViewHolder {
    	public LinearLayout layout;
        public TextView txtName;
        public TextView txtStart;
        public TextView playBtn;
        public TextView txtEnd;
        public ImageView per1Pic;
        public ImageView per2Pic;
    }

}
